package com.tsunami.auth.controller;

import com.tsunami.auth.service.auth.IApplicationService;
import com.tsunami.authority.dto.auth.ApplicationSaveDTO;
import com.tsunami.authority.dto.auth.ApplicationUpdateDTO;
import com.tsunami.authority.entity.auth.Application;
import com.tsunami.base.R;
import com.tsunami.exception.code.ExceptionCode;
import com.tsunami.log.annotation.SysLog;
import com.tsunami.user.annotation.LoginUser;
import com.tsunami.user.model.SysUser;
import com.tsunami.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname ApplicationController
 * @Description TODO
 * @Date 2022-03-09 17:11
 * @Created by lx
 */
@RestController
@RequestMapping("/application")
@Api(value = "Application", tags = "应用管理")
public class ApplicationController {

    @Autowired
    IApplicationService applicationService;

    @GetMapping("list")
    @ApiOperation(notes = "应用管理列表（分页）",value = "获取应用列表")
    @SysLog("获取应用列表")
    public R list(Integer pageNum,
                  Integer pageSize,
                  String orderByColumn,
                  String isAsc,
                  Application data){
        return applicationService.selectPageList(data);
    }
    /**
     * 新增应用
     */
    @ApiOperation(value = "新增应用", notes = "新增应用不为空的字段")
    @PostMapping("/save")
    @SysLog("新增应用")
    public R save(@LoginUser SysUser user, @Validated ApplicationSaveDTO data) {
        if(applicationService.checkCode(data.getCode())){
            return R.error().exCode(ExceptionCode.HAVE_CODE);
        }
        applicationService.saveApp(data, user.getId());
        return R.ok();
    }

    /**
     * 修改应用
     */
    @ApiOperation(value = "修改应用", notes = "修改应用不为空的字段")
    @PostMapping("/update")
    @SysLog("修改应用")
    public R update(@LoginUser SysUser user, ApplicationUpdateDTO data) {
        applicationService.updateApp(data, user.getId());
        return R.ok();
    }

    /**
     * 删除应用 删除应用会删除角色和权限
     */
    @ApiOperation(value = "删除应用", notes = "根据id物理删除应用")
    @PostMapping("/delete")
    @SysLog("删除应用")
    public R delete(String ids) {
        if(StringUtils.isEmpty(ids)){
            return R.error();
        }
        String[] idss = ids.split(",");
        List<Long> idList = Arrays.stream(idss)
                .map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        applicationService.removeById(idList);
        return R.ok();
    }
    /**
     * 应用下拉列表
     */
    @ApiOperation(value = "应用下拉列表", notes = "应用下拉列表")
    @PostMapping("/down")
    public R down(){
        List<Application> list = applicationService.list();
        return R.ok().data("list",list);
    }
}
